﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pay.Entity;
using pay.BLL;
using System.Collections;
using System.Text;

public partial class test_PayHander : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request["productid"]))
            {
                Response.Write("<script>alert('不存在此产品编号，请重新选择！'); this.history.back(-1);</script>");
                return;
            }
            if (string.IsNullOrEmpty(Request["uname"]))
            {
                Response.Write("<script>alert('用户姓名不能为空!'); this.history.back(-1);</script>");
                return;
            }
            if (string.IsNullOrEmpty(Request["tel"]))
            {
                Response.Write("<script>alert('联系电话不能为空！!'); this.history.back(-1);</script>");
                return;

            }
            var t = Request["sourceType"];
            if (string.IsNullOrEmpty(Request["sourceType"]))
            {
                sourceType = 1;
            }
            else
            {
                sourceType =Convert.ToInt32( Request["sourceType"]);
            }
            Buy_Product();
        }
    }

  
    public void Buy_Product()
    {
        List<ShopCard> lst = null;
        ShopCard sc = null;
       
        uname = Request["uname"].ToString();
        tel = Request["tel"].ToString();
        try
        {
            if (Session["EmoneyShopCard"] == null)
            {
                lst = new List<ShopCard>();
                sc = new ShopCard();

                if (string.IsNullOrEmpty(Request["count"]))
                {
                    sc.Count = 1;
                }
                else
                {
                    sc.Count = Convert.ToInt32(Request["count"]);
                }
                sc.ProductId = Convert.ToInt32(Request["productid"].ToString());
                lst.Add(sc);
                Session["EmoneyShopCard"] = lst;

            }
            else
            {
                lst = (List<ShopCard>)Session["EmoneyShopCard"];
                bool flag = true;

                for (int i = 0; i < lst.Count; i++)
                {
                    sc = lst[i];
                    if (Convert.ToInt32(Request["productid"]) == sc.ProductId)
                    {
                        if (string.IsNullOrEmpty(Request["count"])) //数量
                        {
                            sc.Count+= 1;
                        }
                        else
                        {
                            sc.Count += Convert.ToInt32(Request["count"]);
                        }
                        flag = false;
                    }
                }
                if (flag)
                {
                    sc = new ShopCard();
                    if (string.IsNullOrEmpty(Request["count"]))
                    {
                        sc.Count = 1;
                    }
                    else
                    {
                        sc.Count = Convert.ToInt32(Request["count"]);
                    }
                    sc.ProductId = Convert.ToInt32(Request["productid"]);
                    lst.Add(sc);
                }
                Session["EmoneyShopCard"] = lst;

            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "'); this.history.back(-1);</script>");
        }

    }

    public string uname 
    {
        get;
        set;
    }
    public string tel
    {
        get;
        set;
    }
    public int sourceType
    {
        get;
        set;
    } //来源

}